home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471323519
Publication Date: 11/01/98

Search this book:
 
Previous Table of Contents Next


Review the code and decide how to add error handlers to it. Appendix A, “Self-Test Solutions,” contains an improved version of the program that uses proper error handling to protect the program.

Option Explicit


' Load cboPatterns with file selection patterns.
Private Sub Form_Load()
    cboPatterns.AddItem "Bitmaps (*.bmp)"
    cboPatterns.AddItem "GIF (*.gif)"
    cboPatterns.AddItem "JPEG (*.jpg)"
    cboPatterns.AddItem "Graphic (*.bmp;*.gif;*.jpg)"
    cboPatterns.AddItem "All Files (*.*)"

    ' Start with the first choice (bitmaps) selected.
    cboPatterns.ListIndex = 0
End Sub

' Make the controls as big as possible.
Private Sub Form_Resize()
Const GAP = 60

Dim wid As Integer
Dim hgt As Integer

    ' Put drvDrives at the top.
    wid = drvDrives.Width
    drvDrives.Move GAP, GAP, wid

    ' Put cboPatterns at the bottom.
    cboPatterns.Move GAP, ScaleHeight - cboPatterns.Height, wid

    ' Make dirDirectories and filFiles split the
    ' rest of the available height.
    hgt = (cboPatterns.Top - drvDrives.Top - _
        drvDrives.Height - 3 * GAP) / 2
    dirDirectories.Move GAP, drvDrives.Top + _
        drvDrives.Height + GAP, wid, hgt
    filFiles.Move GAP, dirDirectories.Top + _
        dirDirectories.Height + GAP, wid, hgt
End Sub

' The user selected a new pattern.
' Make filFiles use the selected pattern to list files.
Private Sub cboPatterns_Click()
Dim choice As String
Dim file_pattern As String
Dim start_pos As Integer
Dim end_pos As Integer
    

    ' Find the pattern between parentheses.
    choice = cboPatterns.List(cboPatterns.ListIndex)
    start_pos = InStr(choice, "(")
    end_pos = InStr(choice, ")")
    file_pattern = Mid$(choice, start_pos + 1, _
        end_pos - start_pos - 1)

    filFiles.Pattern = file_pattern
End Sub

' The user has picked a new drive.
' Update the directory list.
Private Sub drvDrives_Change()
    dirDirectories.Path = drvDrives.Drive
End Sub

' The user has picked a new directory.
' Update the file list.
Private Sub dirDirectories_Change()
    filFiles.Path = dirDirectories.Path
End Sub

' The user has picked a file.
' Display it.
Private Sub filFiles_Click()
Dim fname As String

    ' Display the selected file's full name
    ' in the form's caption.
    fname = filFiles.Path + "\" + filFiles.filename
    Caption = "Picture Viewer [" & fname & "]"

    ' Display the picture.
    MousePointer = vbHourglass
    DoEvents
    imgView.Picture = LoadPicture(fname)
    MousePointer = vbDefault
End Sub

Summary

The Bug Stoppers that follow summarize the fundamental error handling concepts explained in this chapter. Probably the most important is to always be prepared to handle any kind of error. Even the most thoroughly debugged application can experience all sorts of strange errors through no fault of its own. Be ready to handle these unforeseen errors with robust, carefully tested error handlers.

BUG STOPPERS: Fundamentals
Use On Error statements.
Leave error handlers using Resume, Resume Next, Exit, or Err.Raise, not End.
Define error constants using an error base.
Keep error handlers separate.
Understand error handler scope.
Do not install an error handler within another error handler.
Write bugproof error handlers.
Always be prepared for errors.
Make routines clean up their messes.
Handle errors in your objects.
Handle errors in objects you don’t create.
Use the project options to break on errors.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.